package com.client.data;


import com.shared.model.Compagnie;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
/**
 * Extension d'une {@link ListGrid} aux {@link Compagnie}
 * @author queyroi
 *
 */
public class CompagnieListGrid extends ListGrid{
	
	
	public CompagnieListGrid() {
				
		ListGridField nomField = new ListGridField("nom", "Nom");
		nomField.setWidth(100);
		ListGridField adresse1Field = new ListGridField("adresse1", "Adresse princ.");
		adresse1Field.setWidth(200);
		ListGridField adresse2Field = new ListGridField("adresse2", "Adresse sec.");
		adresse2Field.setWidth(200);
		ListGridField numeroTelField = new ListGridField("numeroTel", "Numero Tel");
		numeroTelField.setWidth(200);
		ListGridField numeroFaxField = new ListGridField("numeroFax", "Numero Fax");
		numeroTelField.setWidth(200);
		
		ListGridField[] fields = new ListGridField[]{nomField,adresse1Field, adresse2Field,
				numeroTelField, numeroFaxField};
		
		setFields(fields);
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setHeight("300px");
		setAutoFitData(Autofit.HORIZONTAL);
		setAutoFitMaxWidth(670);		
		setCellHeight(40);
		setWrapCells(true);
	}
	
	public void addData(Compagnie data) {		
		ListGridRecord record = new ListGridRecord();
		record.setAttribute("nom", data.getNom());
		if(data.getAdresse1()!=null){
			record.setAttribute("adresse1",  data.getAdresse1().getAdresse());		
		}else{
			record.setAttribute("adresse1", "");	
		}
		if(data.getAdresse1()!=null){
			record.setAttribute("adresse2",  data.getAdresse2().getAdresse());		
		}else{
			record.setAttribute("adresse2", "");	
		}		
		record.setAttribute("numeroTel", data.getNumeroTel());
		record.setAttribute("numeroFax", data.getNumeroFax());		
		addData(record);
	}
	
	public void removeAll(){
		ListGridRecord[] list = getRecords();
		for(Record r : list) this.removeData(r);
	}

}
